//
//  MyCollectShopContentVC.m
//  BusinessMarket
//
//  Created by Michael Li on 2018/12/2.
//  Copyright © 2018年 Michael Li. All rights reserved.
//

#import "MyCollectShopContentVC.h"
#import "MyShopHeadView.h"
#import "MyShopSearchView.h"
#import "MyCollectShopDemandTabVC.h"
#import "MyCollectShopServiceTabVC.h"
#import "ServiceModel.h"
#import "HomeModel.h"
#import "UIView+TYAlertView.h"
#import "ShowIntroduceView.h"
#import "ShopSubModel.h"
#import "ServiceContentTabVC.h"
#import "DemandTheHallContentVC.h"
#import "MyShopSearchTabVC.h"
@interface MyCollectShopContentVC ()<MyShopHeadViewDelegate,UIScrollViewDelegate,MyShopSearchViewDelegate,MyShopSearchTabVCDelegate>
@property(nonatomic,strong) NSDictionary *dict;
@property(nonatomic,strong) AFHTTPSessionManager *manager;
@property(nonatomic,strong) MCButton *preBtn;
@property(nonatomic,assign) NSInteger btnTag;
@property(nonatomic,strong) UIButton *sureBtn;
@property(nonatomic,strong) MyShopHeadView *headView;
@property(nonatomic,strong) UIView *shoeView;
@property(nonatomic,strong) ShowIntroduceView *showIntroduceView;
@property(nonatomic,strong) NSString *urlString;
@property(nonatomic,strong) MyShopSearchView *searchView;
@property(nonatomic,strong) MyCollectShopDemandTabVC *demandVC;
@property(nonatomic,strong) MyCollectShopServiceTabVC *serviceVC;
@property(nonatomic,strong) UIScrollView *scrollView;
@property(nonatomic,strong) MyShopSearchTabVC *myShopSearchVC;
@end
@implementation MyCollectShopContentVC
- (void)viewDidLoad {
    [super viewDidLoad];
    self.manager = [AFHTTPSessionManager manager];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = [UIColor whiteColor];
    self.headView = [MyShopHeadView createViewFromNib];
    self.headView.frame = CGRectMake(0, 0, kVWidth,  140+kNavAndStatusHight+kStatusBarHeight*2);
    [self.view addSubview:self.headView];
    self.headView.delegate = self;
    self.headView.ShopSubModel = self.model;
    self.searchView = [[MyShopSearchView alloc]initWithFrame:CGRectMake(0, 140, kVWidth, 45)];
    self.searchView.delegate = self;
    self.searchView.index = 2;
    [self.view addSubview:self.searchView];
    
    self.sureBtn = [[UIButton alloc]initWithFrame:CGRectMake(kUIScreen.size.width*0.05, kUIScreen.size.height-50, kUIScreen.size.width*0.9, 44)];
    [self.view addSubview:self.sureBtn];
    [self.sureBtn addTarget: self action:@selector(nextPage) forControlEvents:UIControlEventTouchUpInside];
    [self.sureBtn setBackgroundColor:kOrangeBg];
    self.sureBtn.layer.masksToBounds = YES;
    self.sureBtn.layer.cornerRadius = 8;
    self.sureBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [self.sureBtn setTitle:@"联系店主" forState:UIControlStateNormal];
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.searchView.frame), kVWidth, kVHeight-CGRectGetMaxY(self.searchView.frame)-kNavAndStatusHight)];
    self.scrollView.pagingEnabled = YES;
    self.scrollView.delegate = self;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.scrollView];
    self.myShopSearchVC = [[MyShopSearchTabVC alloc]init];
    self.myShopSearchVC.delegate = self;
    self.myShopSearchVC.view.frame = CGRectMake(0, CGRectGetMaxY(self.searchView.frame), kVWidth, kVHeight-CGRectGetMaxY(self.searchView.frame));
    self.myShopSearchVC.view.hidden = YES;
    [self.view addSubview:self.myShopSearchVC.view];
    self.scrollView.contentSize = CGSizeMake(kVWidth*2, 0);
    self.serviceVC = [[MyCollectShopServiceTabVC alloc]init];
    self.serviceVC.model = self.model;
    __weak typeof (self)weakSelf = self;
     self.serviceVC.view.frame = CGRectMake(0, 0, kVWidth, self.scrollView.frame.size.height);
    self.serviceVC.returnValueBlock = ^(ServiceModel * _Nonnull model) {
        ServiceContentTabVC *vc = [[ServiceContentTabVC alloc]init];
        vc.model = model;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    };
    [self.scrollView addSubview:self.serviceVC.view];
    self.demandVC = [[MyCollectShopDemandTabVC alloc]init];
    self.demandVC.model = self.model;
    self.demandVC.returnValueBlock = ^(HomeModel * _Nonnull homeModel) {
        DemandTheHallContentVC *vc = [[DemandTheHallContentVC alloc]init];
        vc.homeModel = homeModel;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    };
    self.demandVC.view.frame = CGRectMake(kVWidth, 0, kVWidth, self.scrollView.frame.size.height);
    [self.scrollView addSubview:self.demandVC.view];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(collectionupdateTime:) name:@"collectionupdateTime" object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(qingqiusibai:) name:@"qingqiusibai" object:nil];
    self.btnTag = 1;
    [self reloadInternetShop];
}
-(void)qingqiusibai:(NSNotification *)info
{
    NSDictionary *dict = info.userInfo;
    XLog(@"dict:%@",dict);
//    NSString *message = [info.userInfo objectForKey:@"message"];
//    [WSProgressHUD showImage:[UIImage imageNamed:@""] status:message];
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        [WSProgressHUD dismiss];
//    });
}

-(void)collectionupdateTime:(NSNotification *)info
{
    XLog(@"info:%@",info);
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
#pragma mark-代理:取消店铺更新的信息
-(void)reloadInternetShop
{
    self.dict = @{
                  @"uid":[UserDefaultsHelper readUid],
                  @"token":[UserDefaultsHelper readToken]
                  };
    [CustomeManager POSTWithURLIndex:49 parameters:self.dict success:^(NSDictionary *dict) {
        
    } failure:^(NSError *error) {
        
    }];
}
#pragma mark-代理:店铺内搜索
- (void)myShopSearchView:(MyShopSearchView *)myShopSearchView withClickBtn:(nonnull UIButton *)clickBtn withSearchText:(nonnull NSString *)searchText
{
    
    XLog(@"clickBtn.tag:%ld",clickBtn.tag);
    if (clickBtn.tag != 5) {
        self.myShopSearchVC.view.hidden = NO;
        self.myShopSearchVC.searchText = searchText;
    }else {
        self.myShopSearchVC.view.hidden = YES;
    }
    
}
#pragma mark-代理:点击 服务列表,需求列表
- (void)myShopSearchView:(MyShopSearchView *)myShopSearchView withClickBtn:(UIButton *)clickBtn
{
    [WSProgressHUD setProgressHUDIndicatorStyle:WSProgressHUDIndicatorBigGray];
    [WSProgressHUD show];
    clickBtn.tag?[self.scrollView setContentOffset:CGPointMake((clickBtn.tag-1)*kVWidth, 0) animated:NO]:0;
    switch (clickBtn.tag) {
        case 0:
        {
            [WSProgressHUD dismiss];
        }
            break;
        case 1:
        {
            self.serviceVC.serviceArray = [NSMutableArray array];
            self.serviceVC.row = 10;
            self.serviceVC.page = 1;
            [self.serviceVC reloadInternetData];
        }
            break;
        case 2:
        {
            self.demandVC.demandArray = [NSMutableArray array];
            self.demandVC.row = 10;
            self.demandVC.page = 1;
            [self.demandVC reloadInternetData];
        }
            break;
        default:
            break;
    }
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x/kVWidth;
    MCButton *btn = (MCButton *)[self.searchView viewWithTag:index+1];
    [self.searchView clickBtn:btn];
}

#pragma mark-点击:服务列表,需求列表
-(void)clickBtn:(MCButton *)clickBtn
{
    if (clickBtn.tag == 0) {
    }else if (clickBtn.tag == 1) {

    }else {

    }
    if (clickBtn.tag != 0) {
        if (self.preBtn == clickBtn) {
            [clickBtn setImage:[ImageColorHelper createImageWithColor:kOrangeBg] forState:UIControlStateNormal];
            [clickBtn setTitleColor:kLabel16Color forState:UIControlStateNormal];
        }else {
            [clickBtn setImage:[ImageColorHelper createImageWithColor:kOrangeBg] forState:UIControlStateNormal];
            [clickBtn setTitleColor:kLabel16Color forState:UIControlStateNormal];
            [self.preBtn setTitleColor:kLabel153Color forState:UIControlStateNormal];
            [self.preBtn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
        }
        self.preBtn = clickBtn;
        self.btnTag  = clickBtn.tag;
    }
}

#pragma mark-代理:点击客户评价,收藏
- (void)myShopHeadView:(MyShopHeadView *)myShopHeadView withCollectBtn:(nonnull UIButton *)collectBtn withShopSubModel:(nonnull ShopSubModel *)shopSubModel
{
    if (collectBtn.tag == 0) {
        self.showIntroduceView = [[ShowIntroduceView alloc]initWithFrame:CGRectMake(20, 0, kVWidth*0.8, kVHeight*0.6)];
        
        self.showIntroduceView.model = self.model;
         [self.showIntroduceView showInWindow];
    }else {
        UIViewController* viewController = [UIApplication sharedApplication].keyWindow.rootViewController;
        if ([[UserDefaultsHelper readAutoLogIn] isEqualToString:@"NO"]||[UserDefaultsHelper readAutoLogIn].length == 0) {
            LogInOrOutVC *logVC = [[LogInOrOutVC alloc]init];
            MainNavC *mianNav = [[MainNavC alloc]initWithRootViewController:logVC];
            [viewController presentViewController:mianNav animated:YES completion:nil];
        }else if (shopSubModel.uid == [[UserDefaultsHelper readUid]integerValue]) {
            [WSProgressHUD showImage:[UIImage imageNamed:@""] status:@"不可以收藏自己的店铺"];
        }else {
            self.urlString = [NSString stringWithFormat:@"%@/collection/insertCollection",kUrl];
            NSString *ids = [NSString stringWithFormat:@"%ld",shopSubModel.id];
            collectBtn.selected =  !collectBtn.selected;
            self.dict = @{
                          @"uid":[UserDefaultsHelper readUid],
                          @"id":ids,
                          @"type":@"3",
                          @"token":[UserDefaultsHelper readToken],
                          @"status":[NumberSwitchString numberSwitchString:collectBtn.selected]
                          };
            XLog(@"dict:%@",self.dict);
            [self.manager POST:self.urlString parameters:self.dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                XLog(@"responseObject:%@",responseObject);
                NSString *message = [responseObject objectForKey:@"message"];
                [WSProgressHUD showImage:[UIImage imageNamed:@""] status:message];
                if ([message containsString:@"收藏成功"]) {
                    shopSubModel.collectStatus = 1;
                    [collectBtn setTitle:@"收藏成功" forState:UIControlStateNormal];
                }else {
                    shopSubModel.collectStatus = 0;
                    [collectBtn setTitle:@"添加收藏" forState:UIControlStateNormal];
                }
                NSNotification *notification = [NSNotification notificationWithName:@"collectShop" object:nil];
                [[NSNotificationCenter defaultCenter]postNotification:notification];
                
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                
            }];
            
        }
    }
}

@end
